package com.agiantii.ablog.handler.article.filter;

import com.agiantii.ablog.handler.article.filter.impl.*;
import com.agiantii.ablog.pojo.dto.PostDTO;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.annotation.PostConstruct;
import java.util.Iterator;

@Slf4j
@Component
public class ContentFilterManager {
    
    @Resource
    private FilterCollection filterCollection;
    
    @Resource
    private TitleFilter titleFilter;
    
    @Resource
    private WordCountFilter wordCountFilter;
    
    @Resource
    private SensitiveWordFilter sensitiveWordFilter;
    
    @Resource
    private ImageFilter imageFilter;
    
    @PostConstruct
    public void init() {
        filterCollection.clear();
        
        filterCollection.addFilter(titleFilter);
        filterCollection.addFilter(wordCountFilter);
        filterCollection.addFilter(sensitiveWordFilter);
        filterCollection.addFilter(imageFilter);
        
        log.info("Content filters initialized, total filters: {}", filterCollection.size());
    }
    
    public void filterContent(PostDTO content) {
        if (content == null) {
            throw new IllegalArgumentException("Content cannot be null");
        }
        
        Iterator<ContentFilter> iterator = filterCollection.createIterator();
        while (iterator.hasNext()) {
            ContentFilter filter = iterator.next();
            try {
                filter.doFilter(content);
            } catch (Exception e) {
                log.error("Filter {} failed: {}", filter.getName(), e.getMessage());
                throw e;
            }
        }
    }
} 